<!--
  商品登记: 做到向后台商品表中添加商品 库存默认是0
-->
<script>
export default {
  name: "GoodsInfoLogin",
  data() {
    return {
      allGoodsType_Login: [],
      ruleForm: {
        name: '',
        typeId: '',
        money: '',
        imageUrl: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入商品名称', trigger: 'blur'}
        ],
        typeId: [
          {required: true, message: '请选择商品类型', trigger: 'blur'}
        ],
        money: [
          {required: true, message: '请输入商品价格', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 上传图片成功之后执行
    handleAvatarSuccess(res, file) {
      this.ruleForm.picture = res.data;
      this.ruleForm.imageUrl = URL.createObjectURL(file.raw);
    },
    // 上传图片之前执行
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    submitForm() {
      console.info(this.ruleForm)
      this.ruleForm.money = this.ruleForm.money + "元"
      this.$http.post("/goods/addGoods", this.ruleForm)
          .then(res => {
            if (res.data.msg === 1) {
              this.$message.success("商品登记成功")
            } else {
              this.$message.error("商品登记失败")
            }
          })

    },
    // 查询所有商品类型
    findAllGoodsType() {
      this.$http.get("/goods/findAllType")
          .then((res) => {
            this.allGoodsType_Login = res.data;
          });
    },
  },
  created() {
    this.findAllGoodsType()
  }
}
</script>

<template>
  <div>
    <h2 style="margin-left: 100px">登记商品</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name" class="addFormWidth"/>
      </el-form-item>

      <el-form-item label="商品类型" prop="typeId">
        <el-select v-model="ruleForm.typeId" placeholder="请选择该商品的类型" class="addFormWidth">
          <el-option :label="item.name" :value="String(item.id)" v-for="item in allGoodsType_Login" :key="item.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="商品价格" prop="money">
        <el-input v-model="ruleForm.money" class="addFormWidth"/>
      </el-form-item>

      <el-form-item label="商品图片" prop="desc">
        <el-upload class="avatar-uploader"
                   action="http://localhost:2001/goods/addGoodsPicture"
                   :with-credentials="true"
                   :show-file-list="false"
                   :on-success="handleAvatarSuccess"
                   :before-upload="beforeAvatarUpload">
          <!-- ruleForm.imageUrl 图片的路径-->
          <el-image v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar"/>
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>

      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.addFormWidth {
  width: 300px;
}

.el-form {
  width: 500px;
  height: 800px;
}


.avatar, .avatar-uploader {
  width: 200px;
  height: 200px;
  display: block;
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader :hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
}
</style>